<script setup>
import api from '@/api'
import { ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import dayjs from 'dayjs'
import { showToast } from 'vant'

const route = useRoute()
const router = useRouter()

const material = ref(null)
const loading = ref(false)

const getMaterial = async () => {
  loading.value = true
  try {
    material.value = await api.get(`ip/materials/` + route.params.id)
  } catch (error) {
    console.error(error)
  }
  loading.value = false
}

const copy = async () => {
  try {
    await navigator.clipboard.writeText(material.value.link)
    showToast('已复制')
  } catch (error) {
    showToast('复制失败，您的浏览器不支持直接复制')
  }
}

const favorite = async () => {
  await api.post(`ip/materials/${material.value.id}/favorite`)
  if (material.value.favorited) {
    material.value.favorited = false
    material.value.favorite_count -= 1
  } else {
    material.value.favorited = true
    material.value.favorite_count += 1
  }
}

getMaterial()
</script>

<template>
  <div>
    <van-loading v-if="loading" class="page-loading" type="spinner" />

    <van-empty description="素材未找到" v-else-if="!material" class="page-empty" />

    <div class="container" v-else>
      <div class="header">
        <div class="image">
          <img :src="material.image" />
        </div>
        <div class="body">
          <div class="title">{{ material.title }}</div>
          <div class="footer">
            <div class="footer1">
              <div class="from">来源：{{ material.from }}</div>
              <div class="time">{{ dayjs(material.created_at).format('YYYY-MM-DD HH:mm') }}</div>
            </div>
            <div class="footer2">
              <div class="category-name">{{ material.category.name }}</div>
              <div class="count">
                <img src="@/assets/img/sc.png" />
                {{ material.favorite_count }}
              </div>

              <div class="count">
                <img src="@/assets/img/tk.png" />
                {{ material.video_count }}
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="btn-copy-link" id="btn-copy-link" @click="copy(source)">一键复制链接</div>

      <div class="content">
        {{ material.content }}{{ material.content }}{{ material.content }}{{ material.content }}{{ material.content }}{{ material.content }}{{ material.content }}{{ material.content }}
      </div>

      <div class="tabs">
        <div class="tab" @click="favorite">
          <template v-if="material.favorited">
            <img src="@/assets/img/shoucang_s.png" />
            <div style="color: #ffbe40">已收藏</div>
          </template>
          <template v-else>
            <img src="@/assets/img/shoucang_n.png" />
            <div>收藏</div>
          </template>
        </div>
        <div class="tab" @click="$router.push(`/ip/materials/${material.id}/record`)">
          <img src="@/assets/img/paitongkuan.png" />
          <div style="color: #144bbb">拍同款</div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.container {
  .header {
    display: flex;
    padding: 4vw;

    .image {
      width: 28.8vw;
      height: 48.5vw;
      flex-shrink: 0;
      display: flex;
      align-items: center;
      justify-content: center;
      img {
        display: block;
        max-width: 100%;
        max-height: 100%;
        border-radius: 6px;
      }
    }

    .body {
      padding-left: 4vw;
      display: flex;
      flex-direction: column;
      .title {
        font-family: PingFangSC-Regular;
        font-size: 4.2vw;
        color: #333333;
        display: -webkit-box;
        -webkit-line-clamp: 4;
        -webkit-box-orient: vertical;
        overflow: hidden;
        text-overflow: ellipsis;
        height: 22vw;
      }
      .footer {
        margin-top: auto;
        .footer1 {
          display: flex;
          align-items: center;
          color: #c4c3c3;

          .from {
            font-size: 2.5vw;
            border: 0.3vw solid #c4c3c3;
            border-radius: 50px;
            flex-shrink: 0;
            padding: 2vw 2vw;
            line-height: 0;
          }

          .time {
            font-size: 3.5vw;
            margin-left: 3vw;
          }
        }
        .footer2 {
          display: flex;
          align-items: center;
          margin-top: 2.5vw;
          .category-name {
            border: 1px solid #ccb29e;
            font-family: PingFangSC-Regular;
            border-radius: 2px;
            padding: 0.8vw;
            font-size: 2.5vw;
            color: #ccb29e;
            line-height: 1;
          }

          .count {
            display: flex;
            align-items: center;
            font-family: PingFangSC-Light;
            font-size: 2.5vw;
            color: #9c9b9b;
            font-weight: 200;
            margin-left: 4vw;
            img {
              display: block;
              width: 3.2vw;
              margin-right: 0.8vw;
            }
          }
        }
      }
    }
  }

  .btn-copy-link {
    background: #f7f7f7;
    border-radius: 50px;
    margin: 4vw;
    font-family: PingFangSC-Medium;
    font-size: 4vw;
    color: #144bbb;
    text-align: center;
    font-weight: 500;
    padding: 3vw 0;
  }

  .content {
    margin: 4vw;
    margin-top: 8vw;
    padding-top: 6vw;
    border-top: 0.2vw solid #f7f7f7;

    font-family: PingFangSC-Regular;
    font-size: 4vw;
    color: rgba(0, 0, 0, 0.6);
    letter-spacing: 0;
    text-align: justify;
    font-weight: 400;
    word-break: break-all;
    white-space: pre-wrap;
    padding-bottom: 20vw;
  }

  .tabs {
    border-top: 1px solid #ebebeb;
    position: fixed;
    bottom: 0;
    width: 100%;
    background: #fff;
    z-index: 99;
    display: flex;
    color: #8593ab;
    padding: 2vw 0;
    .tab {
      display: flex;
      flex-direction: column;
      align-items: center;
      flex: 1;
      flex-shrink: 0;
      font-size: 3vw;
      img {
        width: 5.3vw;
        display: block;
        margin-bottom: 1vw;
      }
    }
  }
}
</style>
